//用于测试计划将TabPanel所以内容清空以供新建操作
function testDesignResetFormForCreate(){
	testDesignCreateOrUpdate = 0;//标注现在是新建操作
	testDesignTabPanel.down('#testDesignCommitButton').setHidden(false);
	testDesignTabPanel.down('#testDesignSaveButton').setHidden(false);
	var item = testDesignLeftTree.getSelectionModel().getSelected();// 得到被选择的行的数组.
	testDesignHiddenTab(!item.items[0].data.leaf,true);//如果新建的是文件夹就只是显示详情页就好了
	testDesignBanEditor(false);
	testDesignDetailForm.down('#testDesignDetailFormPriority').setHidden(!item.items[0].data.leaf);//如果是新建文件夹的话就将优先级下拉框隐藏掉
	testDesignDetailForm.down('#testDesignDetailFormPriority').setConfig('allowBlank',!item.items[0].data.leaf);//如果是点击文件夹的话就将优先级下拉框可以不填
	testDesignDetailForm.down('#testDesignDetailFormState').setHidden(!item.items[0].data.leaf);//如果是点击文件夹的话就将状态显示隐藏掉
	//********************************基本信息填充begin****************************************//
	testDesignDetailForm.down('#testDesignDetailFormTitle').setValue();
	testDesignDetailForm.down('#testDesignDetailFormState').setValue();
	testDesignDetailForm.down('#testDesignDetailFormAuthor').setValue(currentUser);
	testDesignDetailForm.down('#testDesignDetailFormPriority').setValue();
	testDesignDetailForm.down('#testDesignDetailFormDescription').setValue();
	testDesignDetailForm.down('#testDesignDetailFormCreateDate').setValue();
	testDesignDetailForm.down('#testDesignDetailFormUpdateDate').setValue();
	//********************************基本信息填充end****************************************//
	//********************************测试步骤填充begin****************************************//
	testDesignStepStore.removeAll();
	//********************************测试步骤填充end****************************************//
	//********************************关联需求填充begin****************************************//
	testDesignTestRequirmentStore.removeAll();
	//********************************关联需求填充end****************************************//
}

//将测试计划的TabPanel显示到中心区域
function testDesignShowForm(){
	Ext.getCmp('centerRegion').add(testDesignTabPanel);//将编辑测试计划的表单显示到中心区域
}

//点击测试计划菜单某个节点之后之后的操作
function testDesignItemClick(view, record, item, index, e, eOpts){
	Ext.getCmp('centerRegion').removeAll(false);// 先移除原有的items,不然直接添加会叠加,如果设置为true就是会自动销毁之前的组件，之后就无法在使用这个组件了
	var checkLogTabHidden=true;
	if (record.data.isCreate == 1) {//如果点击的节点是新建节点的话
        testDesignCreateOrUpdate = 0;//标注现在是新建操作
        testDesignResetFormForCreate();// 表单改造的可以用于新建操作
        testDesignBanEditor(false);//如果是新建节点就不用禁止操作
	}else{
		testDesignCreateOrUpdate = 1;//标注现在是编辑操作
		var data=testDesignGetDetial(record);
		checkLogTabHidden=data.authorId != currentUserId;//如果点击的节点不是自己的或者是新建的就要把查看审核记录的Tab给隐藏掉
		data.leaf=record.data.leaf;
		testDesignPaddingFormForUpdate(data);
		testDesignBanEditor(!testDesignTabPanel.canUpdate);//如果作者不是自己或者对象处于审核中的状态就要禁止操作
	}
	testDesignHiddenTab(!record.data.leaf,checkLogTabHidden);//如果点击的是文件夹就只是显示详情页就好了
	testDesignDetailForm.down('#testDesignDetailFormPriority').setHidden(!record.data.leaf);//如果是点击文件夹的话就将优先级下拉框隐藏掉
	testDesignDetailForm.down('#testDesignDetailFormPriority').setConfig('allowBlank',!record.data.leaf);//如果是点击文件夹的话就将优先级下拉框可以不填
	testDesignDetailForm.down('#testDesignDetailFormState').setHidden(!record.data.leaf);//如果是点击文件夹的话就将状态显示隐藏掉
	testDesignShowForm();
}

//修改了菜单的标题之后同步到中间的表单
function testDesignSynchronize(text){
	testDesignDetailForm.down('#testDesignDetailFormTitle').setValue(text);
	testDesignDetailForm.down('#testDesignDetailFormState').setValue('未完成');
}

//将测试计划的测试步骤进行排序
function testDesignBorderShot(stepList){
	var newArray=new Array();
	for(var i=0;i<stepList.length;i++){
		if(stepList[i].frontNode=='begin'){
			newArray.push(stepList[i]);
			break;
		}
	}
	for(var i=0;i<stepList.length-1;i++){
		for(var j=0;j<stepList.length;j++){
			if(newArray[i].backNode==stepList[j].id){
				newArray.push(stepList[j]);
				break;
			}
		}
	}
	return newArray;
}

//获取这个节点的详细信息
function testDesignGetDetial(record){
	var result;
    Ext.Ajax.request({//获取当前节点的信息
        url: zuulUrl + '/' + testServiceName + '/common/testDesign/detail/' + record.data.id,
        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            obj.createDate = obj.createDate.replace('T', '  ');
            obj.updateDate = obj.updateDate.replace('T', '  ');
            result = obj;
        },
        failure: function (response, opts) {
        	var obj = Ext.decode(response.responseText);
        	if(obj.code==401||obj.code==402||obj.code==403)
        		Ext.Msg.alert("错误", obj.message);
        	else
        		Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
    return result;
}

//将获取到的数据填充到中间的表单
function testDesignPaddingFormForUpdate(data){
	testDesignTabPanel.down('#testDesignCommitButton').setHidden(data.authorId != currentUserId||data.status=='审核中');//根据作者是不是自己以及是否在审核中来决定按钮是否显示出来
	testDesignTabPanel.down('#testDesignSaveButton').setHidden(data.authorId != currentUserId||data.status=='审核中');//根据作者是不是自己以及是否在审核中来决定按钮是否显示出来
	testDesignTabPanel.canUpdate=data.authorId == currentUserId&&data.status!='审核中';
	//********************************基本信息填充begin****************************************//
	testDesignDetailForm.down('#testDesignDetailFormTitle').setValue(data.title);
	testDesignDetailForm.down('#testDesignDetailFormAuthor').setValue(data.author);
	testDesignDetailForm.down('#testDesignDetailFormDescription').setValue(data.describe);
	testDesignDetailForm.down('#testDesignDetailFormCreateDate').setValue(data.createDate);
	testDesignDetailForm.down('#testDesignDetailFormUpdateDate').setValue(data.updateDate);
	//********************************基本信息填充end****************************************//
	if(data.leaf){//如果是文件夹的话就不用填充后面几个版块
		//********************************基本信息填充begin****************************************//
		testDesignDetailForm.down('#testDesignDetailFormState').setValue(data.status);
		testDesignDetailForm.down('#testDesignDetailFormPriority').setValue(data.priority);
		//********************************基本信息填充end****************************************//
		//********************************测试步骤填充begin****************************************//
		testDesignStepStore.removeAll();//setData之前要先removeAll,不然会堆栈溢出，原因不明
		var afterShotStep=testDesignBorderShot(data.step);
		testDesignStepStore.setData(afterShotStep);
		//********************************测试步骤填充end****************************************//
		//********************************关联需求填充begin****************************************//
		testDesignTestRequirmentStore.removeAll();//setData之前要先removeAll,不然会堆栈溢出，原因不明
		testDesignTestRequirmentStore.setData(data.testReq);
		//********************************关联需求填充end****************************************//
	}
}

//用于隐藏和显示tabPanel的tab
function testDesignHiddenTab(flag,checkLogTabHidden){
	testDesignTabPanel.setActiveTab(0);
	if(flag){
		testDesignTabPanel.tabBar.items.items[1].hide();
		testDesignTabPanel.tabBar.items.items[2].hide();
		testDesignTabPanel.tabBar.items.items[3].hide();
	}else{
		testDesignTabPanel.tabBar.items.items[1].show();
		testDesignTabPanel.tabBar.items.items[2].show();
		if(checkLogTabHidden)
			testDesignTabPanel.tabBar.items.items[3].hide();
		else
			testDesignTabPanel.tabBar.items.items[3].show();
	}
}

//将测试计划Tab中所有可以编辑的操作都禁止掉或者解禁，防止除作者以外的其他人修改
function testDesignBanEditor(flag){
	testDesignDetailForm.down('#testDesignDetailFormTitle').setReadOnly(flag);
	testDesignDetailForm.down('#testDesignDetailFormPriority').setReadOnly(flag);
	testDesignDetailForm.down('#testDesignDetailFormDescription').setReadOnly(flag);
	testDesignStepGrid.down('#testDesignAddStepButton').setDisabled(flag);
	testDesignStepGrid.down('#testDesignDeleteStepButton').setDisabled(flag);
	testDesignStepGrid.down('#testDesignUpStepButton').setDisabled(flag);
	testDesignStepGrid.down('#testDesignDownStepButton').setDisabled(flag);
	testDesignTestRequirementGrid.down('#testDesignReqGridAddButton').setDisabled(flag);
	testDesignTestRequirementGrid.down('#testDesignReqGridDeleteButton').setDisabled(flag);
}

//用于保存现有的测试计划
function testDesignSave(saveOrCommit){
	testDesignTabPanel.down('#testDesignCommitButton').setHidden(true);
	testDesignTabPanel.down('#testDesignSaveButton').setHidden(true);
    if (testDesignDetailForm.isValid()) {//判断各种信息填写完整没有
    	var result=new Object();
    	var basicMsg=new Object();
    	var item = testDesignLeftTree.getSelectionModel().getSelected();// 得到被选择的行的数组.
    	result['userId']=currentUserId;
    	basicMsg['id']=item.items[0].data.id;
    	basicMsg['title']=testDesignDetailForm.down('#testDesignDetailFormTitle').lastValue;
    	basicMsg['author']=currentUserId;
    	basicMsg['describe']=testDesignDetailForm.down('#testDesignDetailFormDescription').lastValue;
    	basicMsg['leaf']=item.items[0].data.leaf?1:0;
    	basicMsg['parentId']=item.items[0].data.parentId;
    	basicMsg['project']=testDesignProject;
    	basicMsg['status']=item.items[0].data.leaf?saveOrCommit:3;
    	result['testDesign']=basicMsg;
    	if(item.items[0].data.leaf){//如果保存不是主题文件夹,就继续保存测试步骤和测试需求和优先级
    		basicMsg['priority']=testDesignDetailForm.down('#testDesignDetailFormPriority').lastValue;
    		var stepArray=new Array();
    		Ext.each(testDesignStepStore.getData().items,function(step){
    			stepArray.push(step.data);
    		});
    		result['step']=stepArray;
    		var testRequirement=new Array();
    		Ext.each(testDesignTestRequirmentStore.getData().items,function(req){
    			testRequirement.push(req.data.id);
    		});
    		result['testReq']=testRequirement;
    	}
    	Ext.Ajax.request({
            url: zuulUrl + '/' + testServiceName + '/common/testDesign',
            method: item.items[0].data.isCreate==1?'post':'put',
            async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            defaultXdrContentType: 'application/json',
            jsonData: JSON.stringify(result),
            success: function (response, opts) {
                //成功了的话要把树菜单的节点文本值更改为表单里填写的文本值
                testDesignStore.getNodeById(item.items[0].data.id).set('text', testDesignDetailForm.down('#testDesignDetailFormTitle').lastValue);
                item.items[0].data.isCreate = 0;//将该节点的isCreate置为0，说明这个节点不再是新建节点
                if (saveOrCommit == 0)//如果是保存操作就显示保存成功
                    Ext.Msg.alert("成功", "保存成功");
                else//不然就显示保存并提交成功
                    Ext.Msg.alert("成功", "保存并提交成功");
                var data = testDesignGetDetial(item.items[0]);//重新获取数据
                data.leaf=item.items[0].data.leaf;//这里要赋值leaf不然如果是叶子节点的话表单的状态不会填充
                testDesignPaddingFormForUpdate(data);//将表单数据重置
            },
            failure: function (response, opts) {
            	var obj = Ext.decode(response.responseText);
            	if(obj.code==401||obj.code==402||obj.code==403)
                	Ext.Msg.alert("错误", obj.message);
                else 
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
            }
        });
    }else
    	Ext.Msg.alert('错误', '测试计划信息请填写完全！');
    testDesignTabPanel.down('#testDesignCommitButton').setHidden(false);
	testDesignTabPanel.down('#testDesignSaveButton').setHidden(false);
	
}

//将编辑器传过来的值赋值到grid
function testDesignPutEditDataIntoGrid(e){
	if(testDesignListenFlag){
		testDesignListenRowData.set('description',e.data);//将编辑器传回来的内容复制到grid对应的行上
		Ext.Msg.alert('成功', '保存成功');
	}
}

//将测试步骤的某一行上移或下移动
function testDesignStepUpOrDown(up){
	var item = testDesignStepGrid.getSelectionModel().getSelected();// 得到被选择的行的数组.
	if(item.length==0)
		Ext.Msg.alert('错误', '请选择要移动的行！');
	else{
		if(up){
	    	if(item.items[0].data.frontNode!='begin'){
	    		var frontNode=testDesignStepStore.getById(item.items[0].data.frontNode);
	    		var backNode=item.items[0].data.backNode=='end'?'end':testDesignStepStore.getById(item.items[0].data.backNode);
	    		item.items[0].data.frontNode=frontNode.data.frontNode;
	    		if(frontNode.data.frontNode!='begin'){//如果这个节点向上移了之后不是最顶端的节点的话就要将frontNode_front的backNode指向自己
	    			var frontNode_front=testDesignStepStore.getById(frontNode.data.frontNode);
	    			frontNode_front.data.backNode=item.items[0].data.id;
	    		}
	    		frontNode.data.frontNode=item.items[0].data.id;
	    		frontNode.data.backNode=item.items[0].data.backNode;
	    		item.items[0].data.backNode=frontNode.data.id;
	    		if(backNode!='end')//如果节点本身就是最后一个节点的话就不用去修改backNode
	    			backNode.data.frontNode=frontNode.data.id;
	    		var record=testDesignStepStore.getById(item.items[0].data.id);
	        	var index=testDesignStepStore.indexOf(record);
	        	testDesignStepStore.removeAt(index);
	        	testDesignStepStore.insert(index - 1, record);
	        	testDesignStepGrid.getView().refresh(); // refesh the row number
	        	testDesignStepGrid.getSelectionModel().select(index - 1);
	    	}
		}else{
	    	if(item.items[0].data.backNode!='end'){
	    		var backNode=testDesignStepStore.getById(item.items[0].data.backNode);
	    		var frontNode=item.items[0].data.frontNode=='begin'?'begin':testDesignStepStore.getById(item.items[0].data.frontNode);
	    		item.items[0].data.backNode=backNode.data.backNode;
	    		if(backNode.data.backNode!='end'){//如果这个节点向下移了之后不是最低端的节点的话就要将backNode_back的frontNode指向自己
	    			var backNode_back=testDesignStepStore.getById(backNode.data.backNode);
	    			backNode_back.data.frontNode=item.items[0].data.id;
	    		}
	    		backNode.data.backNode=item.items[0].data.id;
	    		backNode.data.frontNode=item.items[0].data.frontNode;
	    		item.items[0].data.frontNode=backNode.data.id;
	    		if(frontNode!='begin')//如果节点本身就是第一个节点的话就不用去修改frontNode
	    			frontNode.data.backNode=backNode.data.id;
	    		var record=testDesignStepStore.getById(item.items[0].data.id);
	        	var index=testDesignStepStore.indexOf(record);
	        	testDesignStepStore.removeAt(index);
	        	testDesignStepStore.insert(index +1, record);
	        	testDesignStepGrid.getView().refresh(); // refesh the row number
	        	testDesignStepGrid.getSelectionModel().select(index +1);
	    	}
		}
	}
}

//获取孩子节点
function testDesignReqGetChildren(record){
	if (record.get('leaf') == false && record.get('isHas') == 0) {
        Ext.Ajax.request({
            url: zuulUrl+'/'+testServiceName+'/common/testRequirement/children/'+record.get('id'),
            method: 'get',
            async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            success: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                Ext.each(obj, function (node) {// 获得数据之后先将title的值赋值给text
                    node.text = node.title;
                });
                record.appendChild(obj);
                if (record.data.leaf == false && record.data.expanded == false)
                    record.set('expanded', true);
                record.set("isHas", 1);
            },
            failure: function (response, opts) {
            	var obj = Ext.decode(response.responseText);
            	if(obj.code==401||obj.code==402||obj.code==403)
            		Ext.Msg.alert("错误", obj.message);
            	else
            		Ext.Msg.alert("错误", "该功能暂时不可用！");
            }
        });
    }
}

//将测试计划的节点转换成测试执行
function testDesignTransformToRun(id,name,testLab,priority){
	if(name==null||testLab==''||priority==null)
        Ext.Msg.alert("错误", "转换信息请填写完全！");
	else{
		Ext.Ajax.request({//获取该测试需求的状态描述
	        url: zuulUrl + '/' + testServiceName + '/common/testDesign/transformToRun',
	        method: 'post',
	        async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
	        params: {
	            id: id,
	            name:name,
	            testLab: testLab,
	            priority:priority,
	            author:currentUserId
	        },
	        success: function (response, opts) {
	        	Ext.Msg.alert("成功", "转换成功");
	        },
	        failure: function (response, opts) {
	        	var obj = Ext.decode(response.responseText);
	        	if(obj.code==401||obj.code==402||obj.code==403)
	        		Ext.Msg.alert("错误", obj.message);
	        	else
	        		Ext.Msg.alert("错误", "该功能暂时不可用！");
	        }
	    });
	}
}